package com.softeem.blog.dao;

import java.util.List;

import com.softeem.blog.entity.Ad;
import com.softeem.blog.utils.DBUtils;

public class AdDAO implements IBaseDAO<Ad>{

	@Override
	public boolean insert(Ad t) {
		String sql = "insert into ad(title,summary,path,url,adtime,active,sort) values(?,?,?,?,now(),?,?)";
		return DBUtils.exeUpdate(sql, t.getTitle(),t.getSummary(),t.getPath(),t.getUrl(),t.getActive(),t.getSort());
	} 

	@Override
	public boolean deleteById(int id) {
		String sql = "delete from ad where id=?";
		return DBUtils.exeUpdate(sql, id);
	}

	@Override
	public boolean update(Ad t) {
		String sql = "update ad set title=?,summary=?,path=?,url=? where id=?";
		return DBUtils.exeUpdate(sql, t.getTitle(),t.getSummary(),t.getPath(),t.getUrl(),t.getId()); 
	}

	@Override
	public Ad findById(int id) {
		String sql = "select * from ad where id=?";
		return DBUtils.queryOne(Ad.class, sql, id);
	}

	@Override
	public List<Ad> findByPage(int pageSize, int pageNow) {
		String sql ="select * from ad limit ?,?";
		return DBUtils.queryList(Ad.class, sql, (pageNow-1)*pageSize,pageSize);
	}

	@Override
	public int getCount() {
		String sql = "select count(*) from ad";
		return DBUtils.queryCount(sql);
	}

}
